﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Security;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            if (HttpContext.Current.User.IsInRole("admin")){
                NavigationMenu.Items.Add(new MenuItem("Főoldal", "0", "", "~/Default.aspx"));
                MenuItem series = new MenuItem("Sorozatok", "0", "", "~/Series.aspx");
                series.ChildItems.Add(new MenuItem("Sorozat hozzáadása", "0", "", "~/AddSeries.aspx"));
                series.ChildItems.Add(new MenuItem("Évad hozzáadása", "0", "", "~/AddSeason.aspx"));
                series.ChildItems.Add(new MenuItem("Epizód hozzáadása", "0", "", "~/AddEpisode.aspx"));
                NavigationMenu.Items.Add(series);
                NavigationMenu.Items.Add(new MenuItem("Feliratok", "0", "", "~/Subtitles.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Top 10", "0", "", "~/Top10.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Rólunk", "0", "", "~/About.aspx"));
                MenuItem admin = new MenuItem("Adminisztráció", "0", "", "~/Admin.aspx");
                admin.ChildItems.Add(new MenuItem("Tartalomkezelés", "0", "", "~/Admin.aspx"));
                admin.ChildItems.Add(new MenuItem("Felhasználókezelés", "0", "", "~/AdminUser.aspx"));
                NavigationMenu.Items.Add(admin);
            }
            else if (HttpContext.Current.User.IsInRole("user"))
            {
                NavigationMenu.Items.Add(new MenuItem("Főoldal", "0", "", "~/Default.aspx"));
                MenuItem series = new MenuItem("Sorozatok", "0", "", "~/Series.aspx");
                series.ChildItems.Add(new MenuItem("Sorozat hozzáadása", "0", "", "~/AddSeries.aspx"));
                series.ChildItems.Add(new MenuItem("Évad hozzáadása", "0", "", "~/AddSeason.aspx"));
                series.ChildItems.Add(new MenuItem("Epizód hozzáadása", "0", "", "~/AddEpisode.aspx"));
                NavigationMenu.Items.Add(series);
                NavigationMenu.Items.Add(new MenuItem("Feliratok", "0", "", "~/Subtitles.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Top 10", "0", "", "~/Top10.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Rólunk", "0", "", "~/About.aspx"));
            }
            else{
                NavigationMenu.Items.Add(new MenuItem("Főoldal", "0", "", "~/Default.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Sorozatok", "0", "", "~/Series.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Feliratok", "0", "", "~/Subtitles.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Top 10", "0", "", "~/Top10.aspx"));
                NavigationMenu.Items.Add(new MenuItem("Rólunk", "0", "", "~/About.aspx"));
            }
        }
        else{
            NavigationMenu.Items.Add(new MenuItem("Főoldal", "0", "", "~/Default.aspx"));
            NavigationMenu.Items.Add(new MenuItem("Sorozatok", "0", "", "~/Series.aspx"));
            NavigationMenu.Items.Add(new MenuItem("Feliratok", "0", "", "~/Subtitles.aspx"));
            NavigationMenu.Items.Add(new MenuItem("Top 10", "0", "", "~/Top10.aspx"));
            NavigationMenu.Items.Add(new MenuItem("Rólunk", "0", "", "~/About.aspx"));
        }
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        Login login = ((Login)HeadLoginView.FindControl("LoginUser"));

        if (login.UserName == "" || login.Password == "")
        {
            return;
        }
        else
        {
            ManageDatabase data = new ManageDatabase();
            String userId = data.AuthenticateUser(login.UserName, login.Password);

            if (userId != "" && userId != null)
            {
                HttpCookie authCookie = FormsAuthentication.GetAuthCookie(login.UserName, true);
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
                FormsAuthenticationTicket newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userId);
                authCookie.Value = FormsAuthentication.Encrypt(newTicket);
                Response.Cookies.Add(authCookie);

                String redirectUrl = FormsAuthentication.GetRedirectUrl(login.UserName, true);
                Response.Redirect(redirectUrl);
            }
            else
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }
    }
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        if(!String.IsNullOrEmpty(SearchTextBox.Text))
            Response.Redirect("~/SearchResults.aspx?text=" + Server.UrlEncode(SearchTextBox.Text));
        else
            return;
    }
    //private string RandomImageName()
    //{

    //    Random rand = new Random();

    //    int RandomInt = rand.Next(0, 10);

    //    switch (RandomInt)
    //    {

    //        case 0:
    //            return "~/images/logos/bigbang_logo.png";
    //        case 1:
    //            return "~/images/logos/dexter_logo.png";
    //        case 2:
    //            return "~/images/logos/game_of_thrones_logo.png";
    //        case 3:
    //            return "~/images/logos/sg1_logo.png";
    //        case 4:
    //            return "~/images/logos/sg12_logo.png";
    //        case 5:
    //            return "~/images/logos/sga_logo.png.png";
    //        case 6:
    //            return "~/images/logos/sgu_logo.png";
    //        case 7:
    //            return "~/images/logos/terranova_logo.png";
    //        case 8:
    //            return "~/images/logos/walking_dead_logo.png";
    //        case 9:
    //            return "~/images/logos/walking_dead_logo2.png";
    //        default:
    //            return "~/images/logos/bigbang_logo.png";
    //    }


    //}
}
